<?php

/*
 Website Baker Project <http://www.websitebaker.org/>
 Copyright (C) 2004-2007, Ryan Djurovich

 Website Baker is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Website Baker is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Website Baker; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

require('../../config.php');

// Get id
if(!isset($_GET['marker_id']) OR !is_numeric($_GET['marker_id'])) {
	header("Location: ".ADMIN_URL."/pages/index.php");
} else {
	$marker_id = $_GET['marker_id'];
	$marker = $_GET['marker'];
	$latitude = $_GET['latitude'];
	$longitude = $_GET['longitude'];
	$description = $_GET['description'];
}

// Include WB admin wrapper script
require(WB_PATH.'/modules/admin.php');

// Load Language file
if(LANGUAGE_LOADED) {
    require_once(WB_PATH.'/modules/mapbaker/languages/EN.php');
    if(file_exists(WB_PATH.'/modules/mapbaker/languages/'.LANGUAGE.'.php')) {
        require_once(WB_PATH.'/modules/mapbaker/languages/'.LANGUAGE.'.php');
    }
}

// Get info on marker
$query_marker = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_map_markers WHERE marker_id='$marker_id' AND section_id='$section_id' ");
$fetch_marker = $query_marker->fetchRow();

$latitude = $admin->strip_slashes($fetch_marker['latitude']);
$longitude= $admin->strip_slashes($fetch_marker['longitude']);
$active= $admin->strip_slashes($fetch_marker['active']);

// Get settings
$query_settings = $database->query("SELECT * FROM `".TABLE_PREFIX."mod_map_settings` WHERE section_id='$section_id' LIMIT 1");
$fetch_settings = $query_settings->fetchRow();
// gmaps configuration settings
$gmkey = $admin->strip_slashes($fetch_settings['gmkey']);
$maptype = $admin->strip_slashes($fetch_settings['maptype']);
$deflatitude = $admin->strip_slashes($fetch_settings['deflatitude']);
$deflongitude = $admin->strip_slashes($fetch_settings['deflongitude']);
$defzoom = $admin->strip_slashes($fetch_settings['defzoom']);
$template_infowindow = $admin->strip_slashes($fetch_settings['template_infowindow']);

//

if(intval($latitude)==0 && intval($longitude)==0)
{
	$latitude = $deflatitude;
	$longitude = $deflongitude;
}	

//build template
echo '<script type="text/javascript" src="http://www.google.com/jsapi?key='.$gmkey.'"></script>';
echo '<script type="text/javascript">

		function updateMarker()
		{
			map.clearOverlays();
			var lat = document.getElementById("latitude").value;
			var lng = document.getElementById("longitude").value;			
			var point = new GLatLng(lat,lng);
			marker = new GMarker(point);
			map.addOverlay(marker);			
		}
      
	  var map;
	  var marker;
	  google.load("maps", "2");
      // Call this function when the page has been loaded
	  
		
		function initialize() {
			map = new google.maps.Map2(document.getElementById("map"));
			
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			
			map.setMapType('.$maptype.');

			map.setCenter(new google.maps.LatLng('.$deflatitude.', '.$deflongitude.'), '.$defzoom.');		
			map.enableScrollWheelZoom();		
			var point = new GLatLng('.$latitude.', '.$longitude.');
			marker = new GMarker(point);
			map.addOverlay(marker);

			GEvent.addListener(map,"click", function(marker, latlng) {     
				if (latlng) {
					map.clearOverlays();
					
					document.getElementById("latitude").value = latlng.lat();
					document.getElementById("longitude").value = latlng.lng();
					
					marker = new GMarker(latlng);
					map.addOverlay(marker);
				}
			});
		}
      google.setOnLoadCallback(initialize);
    </script>';
?>

<style type="text/css">
.setting_name {
	vertical-align: top;
}
</style>

<form name="modify" action="<?php echo WB_URL; ?>/modules/mapbaker/save_marker.php" method="post" style="margin: 0;">

<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
<input type="hidden" name="page_id" value="<?php echo $page_id; ?>">
<input type="hidden" name="marker_id" value="<?php echo $marker_id; ?>">

<table class="row_a" cellpadding="2" cellspacing="0" border="0" align="center" width="100%">
<tr>
	<td class="setting_name">
		<label for="category" accesskey="c"><b><?php echo $MBTEXT['CATEGORY']; ?>:</b></label>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<select name="category" id="category" style="width: 98%;">
		<?php
		// Get categories
		$query_categories = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_map_categories WHERE section_id='".$section_id."' ORDER BY pos ASC");
		if($query_categories->numRows() > 0){
			while( $fetch_categories = $query_categories->fetchRow() ) {
				if($fetch_categories['cat_id'] == $fetch_marker['cat_id']){ $selected = "selected"; } else { $selected = ""; }
				echo '<option value="'.$fetch_categories['cat_id'].'" '.$selected.'>'.$admin->strip_slashes($fetch_categories['cat_name']).'</option>';
			}
		}
		?>
		</select>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<label for="marker" accesskey="q"><b><?php echo $MBTEXT['MARKER_NAME']; ?>:</b></label>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<input type="text" name="marker" id="marker" value="<?php echo $admin->strip_slashes($fetch_marker['marker']); ?>" style="width: 98%;" maxlength="255" />
	</td>
</tr>
<tr>
	<td class="setting_name" width="80">
		<label for="cat_name" accesskey="n"><b><?php echo $MBTEXT['ACTIVE']; ?>:</b></label>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<input type="radio" value="1" name="active" <?php if ($active == '1') { echo "checked"; } ?>><?php echo $TEXT['YES'];?>
		<input type="radio" value="0" name="active" <?php if ($active == '0') { echo "checked"; } ?>><?php echo $TEXT['NO'];?>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<label for="latitude" accesskey="q"><b><?php echo $MBTEXT['LATITUDE']; ?>:</b></label>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<input type="text" name="latitude" id="latitude" value="<?php echo $latitude; ?>" style="width: 98%;" maxlength="20" onBlur="updateMarker();" />
	</td>
</tr>
<tr>
	<td class="setting_name">
		<label for="longitude" accesskey="q"><b><?php echo $MBTEXT['LONGITUDE']; ?>:</b></label>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<input type="text" name="longitude" id="longitude" value="<?php echo $longitude; ?>" style="width: 98%;" maxlength="20" onBlur="updateMarker();" />
	</td>
</tr>
<tr>
	<td class="setting_name" colspan="2">
	<?php echo '<div id="map" onmousewheel="cancelscroll()" style="width: 700px; height: 500px; border: 1px solid #000000;"></div>'; ?>
	</td>
</tr>
<tr>
	<td class="setting_name">
		<label for="description" accesskey="q"><b><?php echo $MBTEXT['DESCRIPTION']; ?>:</b></label>
	</td>
</tr>
</table>


<?php
$content = $fetch_marker['description'];
$name="description";

if(!isset($wysiwyg_editor_loaded)) {
	$wysiwyg_editor_loaded=true;

	if (!defined('WYSIWYG_EDITOR') OR WYSIWYG_EDITOR=="none" OR !file_exists(WB_PATH.'/modules/'.WYSIWYG_EDITOR.'/include.php')) {
		function show_wysiwyg_editor($name,$id,$content,$width,$height) {
			echo '<textarea name="'.$name.'" id="'.$id.'" style="width: '.$width.'; height: '.$height.';">'.$content.'</textarea>';
		}
	} else {
		$id_list=array();
		$query_wysiwyg = $database->query("SELECT section_id FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id' AND module = 'mapbaker'");
		if($query_wysiwyg->numRows() > 0) {
			while($wysiwyg_section = $query_wysiwyg->fetchRow()) {
				$entry='content'.$wysiwyg_section['section_id'];
				array_push($id_list,$entry);
			}
			require(WB_PATH.'/modules/'.WYSIWYG_EDITOR.'/include.php');
		}
	}
}

show_wysiwyg_editor('description','content'.$section_id,htmlspecialchars($content),'725px','350px');

?>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
	<td align="left">
		<input name="save" type="submit" value="<?php echo $TEXT['SAVE']; ?>" style="width: 100px; margin-top: 5px;"></form>
	</td>
	<td align="right">
		<input type="button" value="<?php echo $TEXT['CANCEL']; ?>" onclick="javascript: window.location = '<?php echo ADMIN_URL; ?>/pages/modify.php?page_id=<?php echo $page_id; ?>';" style="width: 100px; margin-top: 5px;" />
	</td>
</tr>
</table>

<?php
// Print admin footer
$admin->print_footer();

?>
